<?php
namespace app\admin\controller;
use app\common;
use think\Controller;
use think\Session;
use think\Request;
use think\File;
use think\model;

class First extends common
{
    public function index(){
        $where = '';
        $keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
        $id = (isset($_GET['id'])) ? trim($_GET['id']) : '';
        //搜索条件

        if (($id)) {
            $where = "id='{$id}'";
        }
        //搜索条件
        if ($keyword){
            $where = "title LIKE '%{$keyword}%' OR writer LIKE '%{$keyword}%' OR content LIKE '%{$keyword}%'";
        }

        //查询总数据
        $maxpages = Db('first')->where($where)->count('*');
        //最大页数
        $maxpage = $maxpages / 10;
        // 查询状态为0的数据 并且每页显示10条数据 总记录数为1000
        $mod = db("first")->alias("f")->field("f.id,f.type_id,f.title,f.writer,f.url,f.pic,f.content,f.sort,f.add_time,f.clicks,f.status, onetypes.name type_name")
                    ->join("onetypes","f.type_id = onetypes.id")
                    ->order("f.id")->where($where)->paginate(20, $maxpages);
//        return $this->sucReturn($list);exit;
        // 获取分页显示
        $page = $mod->render();
        // 模板变量赋值
        $this->assign("id",$id);
        $this->assign("keyword",$keyword);
        $this->assign('list', $mod);
        $this->assign('loginname', $this->login());
        $this->assign('page', $page);
        $this->assign('maxpage', $maxpage);
        $this->assign('maxpages', $maxpages);
        $this->assign('empty', '<span class="empty">没有数据</span>');

        // 渲染模板输出
        return $this->fetch('first/index');
    }



    //加载修改页
    public function edit($id = 0)
    {
        //查找要修改的数据
        $info = db("first")->find($id);
        //找出全部分类
        $type = db('onetypes')->select();
//        var_dump($type);exit;
        $this->assign('loginname', $this->login());
        $this->assign('row', $info);
        $this->assign('type', $type);
        return $this->fetch("first/edit");
    }

    //将修改数据的数据保存到数据库

    public function update($id)
    {
        $data['pic']=$_POST['pic'];
        $data['title'] = $_POST['title'];
        $data['content'] = $_POST['content'];
        $data['writer'] = $_POST['writer'];
        $data['type_id'] = $_POST["type_id"];
        $data['status'] = $_POST['status'];
        $data['clicks'] = $_POST['clicks'];
        $data['sort'] = $_POST['sort'];
        $sql = db("first")->where("id", $id)->update($data);
        if ($sql > 0) {
            $this->success("修改成功", "first/index");
        } else {
            $this->error("修改失败");
        }

    }


    //删除数据
    public function delete($id)
    {
        $list = db("first")->find($id);
        if ($list > 0) {
//            unlink($list['url']);
            $sql = db('first')->where("id", $id)->delete();
            if ($sql) {
                $this->success("删除成功", "first/index");
            } else {
                $this->error("删除失败");
            }
        }
    }

}

